//
//  HYHomeFolderListCell.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/5.
//

#import "HYHomeFolderListCell.h"
#import <HYUtls/CommUtls+Time.h>

@implementation HYHomeFolderListCell

- (void)configUI
{
    [super configUI];
    [self.backView addSubview:self.coverView];
    [self.coverView addSubview:self.iconView];
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.dateLabel];
    [self.backView addSubview:self.countLabel];
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.backView);
        make.width.mas_equalTo(kCoverWidth);
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.coverView);
        make.width.height.mas_equalTo(24);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.coverView.mas_right).offset(10);
        make.top.equalTo(self.coverView.mas_top).offset(5);
        make.right.equalTo(self.backView).offset(-49);
    }];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.bottom.equalTo(self.coverView.mas_bottom).offset(-5);
        make.height.mas_equalTo(15);
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.centerY.equalTo(self.coverView);
        make.right.equalTo(self.backView).offset(-5);
        make.height.mas_equalTo(15);
    }];
}

- (void)bindViewModel:(HYHomeFileItemViewModel *)vm
{
    [super bindViewModel:vm];
    if(vm.isLock){
        self.iconView.hidden = NO;
        self.iconView.image = [UIImage imageNamed:@"icon_lock_dark"];
    }else{
        self.iconView.hidden = YES;
    }
    if(vm.fileType == HYHomeFileType_Folder){
        self.coverView.image = [UIImage imageNamed:@"list_folder_mode"];
    }else if(vm.fileType == HYHomeFileType_Pic){
        self.coverView.image = [UIImage imageWithContentsOfFile:vm.filePath];
    }else if (vm.fileType == HYHomeFileType_File){
        self.coverView.image = [UIImage imageNamed:@"icon_file_text"];
    }else if (vm.fileType == HYHomeFileType_Zip){
        self.coverView.image = [UIImage imageNamed:@"icon_file_zip"];
    }else if (vm.fileType == HYHomeFileType_Other){
        self.coverView.image = [UIImage imageNamed:@"icon_file_other"];
    }
    if(vm.playTime){
        self.dateLabel.text = [NSString stringWithFormat:@"%@:%@",NSLocalizedString(@"播放记录", nil),[CommUtls timeToMinMS:@(vm.playTime)]];
    }else if(vm.createDate){
        self.dateLabel.text = vm.createDate;
        if(vm.fileCount){
            self.countLabel.text = [NSString stringWithFormat:@"%ld%@",vm.fileCount,NSLocalizedString(@"项", nil) ];
        }else if(vm.fileSize){
            self.countLabel.text = [CommUtls getSize:@(vm.fileSize)];
        }
    }else{
        if(vm.fileSize){
            self.dateLabel.text = [CommUtls getSize:@(vm.fileSize)];
            if(vm.fileCount){
                self.countLabel.text = [NSString stringWithFormat:@"%ld%@",vm.fileCount,NSLocalizedString(@"项", nil) ];
            }
        }else{
            if(vm.fileCount){
                self.dateLabel.text = [NSString stringWithFormat:@"%ld%@",vm.fileCount,NSLocalizedString(@"项", nil)];
            }else{
                self.dateLabel.text = @"";
            }
        }
    }
    CGFloat titleHeight = [CommUtls fetchTextHeightWithText:vm.fileName
                                                       font:FONT_BOLD_SIZE(14)
                                                 labelWidth:kScreenWidth-15-kCoverWidth-55
                                                  maxHeight:34];
    if(titleHeight > 17){
        [self.countLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.coverView.mas_centerY).offset(10);
        }];
    }else{
        [self.countLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.coverView.mas_centerY);
        }];
    }
}

@end
